<template>
	<view><web-view :src="src" @message="onMessage"></web-view>
	</view>
</template>

<script>
import {appletImgUrl} from "@/common/config.js"
export default {
	data() {
		return {
			appletImgUrl:appletImgUrl,
			src: ''
		};
	},
	onLoad(options) {
		if (options.scene == 'oil') {
			this.scene = options.scene;
			this.src = uni.getStorageSync('oilPayURL');
			return;
		}
		this.src = 'https://out-pay.hiyipin.net/reg/redirect.html?promoter=' + options.promoter + '&openid=' + uni.getStorageSync('openId');
	},
	onUnload() {
		if (this.scene == 'oil') {
			uni.removeStorageSync('oilPayURL');
		}
	},
	methods: {
		updateUserInfo() {
			const self = this;
			self.$u.get('user/info').then(userInfo => {
				if (userInfo.headPhoto) {
					if (userInfo.headPhoto.indexOf('https') != 0) {
						userInfo.headPhoto = this.imageURL + userInfo.headPhoto;
					}
				} else {
					userInfo.headPhoto = this.appletImgUrl+'logo.png';
				}
				uni.setStorageSync('userInfo', userInfo);
			});
		},
		onMessage(e) {
			wx.login({
				success: res => {
					let openId = this.$u
						.post('user/getOpenId', {
							code: res.code,
							xcxName: 'WG_MALL'
						})
						.then(res => {
							uni.setStorageSync('openId', res.openId);
							getApp().globalData.bindState = res.token ? true : false;
							if (res.token) {
								uni.setStorageSync('userToken', {
									id: res.token
								});
								this.updateUserInfo();
							}
						});
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
